<?php


namespace app\common\model;


use think\Model;

class AuthRule extends Model
{
    protected $createTime = 'add_time';
    protected $updateTime = 'up_time';

    public $autoWriteTimestamp = true;

    protected $insert = [
        'status' => 1,//1-启用，0-禁用
    ];



    public function create_data($data)
    {
        return $this->allowField(true)->save($data);
    }

    public function list_data($condition = [],$field=true)
    {
        $map=[];
        if(!empty($condition['status']))$map[]=['status', '=', $condition['status']];
        if(!empty($condition['pid']))$map[]=['pid', '=', $condition['pid']];
        if(!empty($condition['keyword']))$map[]=['name', 'like', '%' . $condition['keyword'] . '%'];
        return self::where($map)
            ->order('sort,id')->field($field)
            ->select()->toArray();
    }

    public function list_data_for_check($condition, $limit = 30)
    {
        return self::where($condition)
            ->order('id asc')
            ->field(true)
            ->limit(0, $limit)
            ->select()
            ->toArray();
    }

    public function update_data($data)
    {
        return $this->allowField(true)->isUpdate(true)->save($data);
    }

    public function fetch_data($id)
    {
        $data = self::get($id);
        return $data ? $data->toArray() : [];
    }

    public function del_data($id)
    {
        return $this->where(['id'=>$id])->delete();
    }

}